package cn.xtits.sync.sdk.customer;

import cn.xtits.sync.sdk.producer.Producer;
import cn.xtits.xtf.common.db.DbConn;
import org.joda.time.DateTime;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class Customer implements Job {

    private static String xtfConnStr = "";

    public static void main(String[] args) {
        System.out.print("参数"+args[0]);
        xtfConnStr=args[0];
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();

            // define the job and tie it to our HelloJob class
            JobDetail job = JobBuilder.newJob(Customer.class)
                    .withIdentity("job1", "group1")
                    .build();

            // 每5秒运行一次job
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("trigger1", "group1")
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInSeconds(5)
                            .repeatForever())
                    .build();

            // Tell quartz to schedule the job using our trigger
            scheduler.scheduleJob(job, trigger);
            TimeUnit.SECONDS.sleep(10);
            scheduler.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static String listSql = "select * from sc_synccommand where FinishFlag=0 and DeleteFlag=0";


    public static void Do() {
        List<Map<String, Object>> list = new ArrayList<>();
        try (DbConn conn = Producer.getConn(xtfConnStr)) {
            list = conn.executeList(listSql);
        } catch (Exception e) {
            System.err.println("获取更新指令失败..." + e.getMessage());
        }
        for (Map<String, Object> item : list) {
            String updateInstance = item.get("UpdateInstance").toString();
            String connStr = item.get("ConnStr").toString();
            //同步数据更新
            try (DbConn conn = Producer.getConn(connStr)) {
                conn.executeNonQuery(updateInstance);
            } catch (Exception e) {
                System.err.println("执行更新指令失败..." + e.getMessage());
            }

            //更新完成状态
            try (DbConn conn = Producer.getConn(xtfConnStr)) {
                conn.executeNonQuery(MessageFormat.format("update sc_synccommand set FinishFlag = 1 where id={0}", item.get("Id").toString()));
            } catch (Exception e) {
                System.err.println("反馈更新指令失败..." + e.getMessage());
            }
        }
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println(DateTime.now().toString("yyyy-MM-dd HH:mm:ss")+"--------------------开始执行--------------------");
        Do();
        System.out.println(DateTime.now().toString("yyyy-MM-dd HH:mm:ss")+"--------------------执行完毕--------------------");

    }
}
